package cc.rengu.igas.smps.biz.service.impl;

import cc.rengu.igas.smps.common.constant.SmpsServiceConstant;
import cc.rengu.igas.smps.common.enums.RespCodeEnum;
import cc.rengu.igas.smps.facade.SmsPlatService;
import cc.rengu.igas.smps.facade.request.GeneralSmsNoticeRequest;
import cc.rengu.igas.smps.facade.response.GeneralSmsNoticeResponse;
import cc.rengu.igas.smps.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

/**
 * Created by WKF on 2020-04-09
 *
 * @author WKF
 */
public class SmsPlatServiceImpl implements SmsPlatService {
    RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    /**
     * 一般短信信息推送
     *
     * @param generalSmsNoticeRequest 一般短信信息推送内容
     * @return 短信平台应答结果
     */
    @Override
    public Result<GeneralSmsNoticeResponse> smsMsgPush(GeneralSmsNoticeRequest generalSmsNoticeRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<GeneralSmsNoticeResponse> result = new Result<>();
        GeneralSmsNoticeResponse generalSmsNoticeResponse = new GeneralSmsNoticeResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(generalSmsNoticeRequest, SmpsServiceConstant.SMS_NOTICE_SERVICE, GeneralSmsNoticeResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((GeneralSmsNoticeResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("短信推送失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                generalSmsNoticeResponse.setRespCode(((BizException) e).getCode());
                generalSmsNoticeResponse.setRespDesc(e.getMessage());
            } else {
                generalSmsNoticeResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                generalSmsNoticeResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

}
